package dd.lo.practice;

import dd.lo.util.ImageUtils;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.commons.lang3.math.NumberUtils;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.text.DecimalFormat;

public class EqualizeHistDisplay extends Application {

    public static void start(String[] args) {
        launch(args);
    }

    private static final int DEFAULT_WIDTH = 1200;
    private static final int DEFAULT_HEIGHT = 900;

    private Mat srcImg;

    private ImageView imageView1;
    private ImageView imageView2;

    private void imageProcess() {
        Task<Void> task = new Task<Void>() {
            @Override
            protected Void call() {
                if (null == srcImg || srcImg.empty()) return null;
                if (srcImg.channels() > 1) {
                    Imgproc.cvtColor(srcImg, srcImg, Imgproc.COLOR_BGR2GRAY);
                }
                Mat displayImg = Mat.zeros(srcImg.cols(), srcImg.rows(), srcImg.type());
                Imgproc.equalizeHist(srcImg, displayImg);
                //显示原图
                ImageUtils.fitImgSize(srcImg, DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 2);
                MatOfByte buffer1 = new MatOfByte();
                Imgcodecs.imencode(".jpeg", srcImg, buffer1);
                Platform.runLater(() -> imageView1.setImage(new Image(new ByteArrayInputStream(buffer1.toArray()))));
                //显示效果图
                ImageUtils.fitImgSize(displayImg, DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 2);
                MatOfByte buffer2 = new MatOfByte();
                Imgcodecs.imencode(".jpeg", displayImg, buffer2);
                Platform.runLater(() -> imageView2.setImage(new Image(new ByteArrayInputStream(buffer2.toArray()))));
                return null;
            }
        };
        new Thread(task).start();
    }

    @Override
    public void start(Stage stage) throws Exception {
        Button imgChooserBtn = new Button("选择图片");
        imgChooserBtn.setOnMouseClicked(e -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("选择图片");
            fileChooser.setInitialDirectory(new File(System.getProperty("user.home") + "/Downloads")
            );
            fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Images", "*.*"));
            File imgFile = fileChooser.showOpenDialog(stage);
            if (null == imgFile) return;
            srcImg = Imgcodecs.imread(imgFile.getAbsolutePath(), Imgcodecs.IMREAD_UNCHANGED);
            if (srcImg.empty()) System.out.println("图片读取失败");
            imageProcess();
        });
        //关闭按钮
        Button exitBtn = new Button("关闭");
        exitBtn.setOnMouseClicked(e -> Platform.exit());
        VBox rows = new VBox(0);
        rows.setPadding(new Insets(10, 10, 0, 10));
        HBox row0 = new HBox(10);
        row0.setPadding(new Insets(5, 0, 5, 0));
        row0.getChildren().addAll(imgChooserBtn, exitBtn);
        rows.getChildren().addAll(row0);
        //对比图展示
        imageView1 = new ImageView();
        imageView2 = new ImageView();
        HBox imgRow = new HBox(10);
        imgRow.setPadding(new Insets(10, 0, 10, 0));
        imgRow.getChildren().addAll(imageView1, imageView2);
        BorderPane root = new BorderPane();
        root.setCenter(imgRow);
        root.setBottom(rows);
        Scene scene = new Scene(root, DEFAULT_WIDTH, DEFAULT_HEIGHT + 50);
        stage.setScene(scene);
        stage.setTitle("直方图均衡化演示");
        stage.setResizable(false);
        stage.show();
    }
}
